package com.example.lib_net.model.api

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import com.example.lib_net.R


/**
 * @author yaotianxue
 * @description
 * @date 2025/2/11
 */
data class PostModel(
    val records: List<PostItem>?,
    val total: Int? ,
    val size: Int?,
    val current: Int?,
    val orders: List<Any>?,
    val optimizeCountSql: Boolean?,
    val searchCount: Boolean?,
    val countId: Any?,
    val maxLimit: Any?,
    val pages: Int?
)
data class PostItem(
    val postsId: String?,
    val nickName: String = "",
    val userName: String?,
    val avatar: ImgItem?,
    val content: String?,
    val img: List<ImgItem>?,
    val series: String?,
    val vehicleModelId: String?,
    val modelNickname: String = "",
    val modelName: String?,
    val channel: Int?,
    val channelStr: String?,
    val virtualShareNumber: Int = 0,
    val virtualLikeNumber: Int = 0,
    val shareNumber: Int = 0,
    var likeNumber: Int = 0,
    val commentNumber: Int = 0,
    var collectNumber: Int = 0,
    val virtualCollectionNumber:Int = 0,
    val ifTipOff: String?,
    val tipOffNumber: Int?,
    val ifSensitive: Int?,
    val topics: List<TopicItem>?,
    val topicStr: Any?,
    val ifRecommend: Int?,
    val ifHot: Int?,
    val weightScore: Double?,
    val categoryStr: String?,
    val recommendSort: Int?,
    val hotSort: Int?,
    val searchSort: Int?,
    val ifUp: Int?,
    val ifUpStr: String?,
    val updateTime: String?,
    val createBy: String?,
    val createTime: String?,
    val listShowTime: String?,
    var ifLike: Int = 0,
    val ifMy: Int?,
    var ifCollect: Int = 0,
    val lon: Double?,
    val lat: Double?,
    val location: String?,
    val ifAudit: Int?,
    val ifVideo: Int?,
    val ifTranscode: Any?,
    val ifAllSeries: Any?,
    val ifAllVehicleModel: Any?,
    val ifAftersale: Int?,
    val aftersaleStatus: Int?,
    val aftersaleResult: Any?,
    val updateBy: String?,
    val updateName: String?,
    val aftersaleTime: Any?,
    val delFlag: Boolean?,
    var relation: Int = 0 ,//好友关系(0:无关系 1:粉丝 2:已关注 3:相互关注)
    // 是否需要展示关注状态，某些特定页面是不展示的
    var needShowFollowState: Boolean? = null
){

    //未关注-->关注 NONE->FOCUS   FANS->FRIENDS
    fun  onFollow(){
        relation = when(relation){
            UserRelationEnum.NONE.relation -> UserRelationEnum.FOCUS.relation
            UserRelationEnum.FANS.relation ->  UserRelationEnum.FRIENDS.relation
            else -> UserRelationEnum.FOCUS.relation
        }
    }
    //关注-->取消关注  FOCUS->NONE   FRIENDS->FANS
    fun onCancelFollow(){
        relation = when(relation){
            UserRelationEnum.FRIENDS.relation -> UserRelationEnum.FANS.relation
            UserRelationEnum.FOCUS.relation ->  UserRelationEnum.NONE.relation
            else -> UserRelationEnum.NONE.relation
        }
    }
    //是否关注
    fun hasFollow():Boolean{
        return relation in listOf(
            UserRelationEnum.FOCUS.relation,
            UserRelationEnum.FRIENDS.relation
        )
    }
    // 获取反收藏状态,用于点击收藏按钮时使用
    fun reverseIfCollect(): Int {
        return if (ifCollect == 1) 0 else 1
    }
    //格式化点赞数显示
    fun formatLikeNumber(): String {
        val total = likeNumber + virtualLikeNumber
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化收藏数显示
    fun formatCollectionNumber(): String {
        val total = collectNumber + virtualCollectionNumber
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }
    //格式化评论数
    fun formatCommentNumber(): String {
        return if (commentNumber < 10_000) {
            commentNumber.toString()
        } else if (commentNumber > 10_000) {
            String.format("%sw+", commentNumber / 10_000)
        } else {
            "1w"
        }
    }
    //格式化分享数显示
    fun formatShareNumber(): String {
        val total = shareNumber + virtualShareNumber
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }
    fun requireAfterSaleLabel(context: Context): Drawable? {
        if (ifAftersale != 1) return null
        return if (aftersaleStatus == 2) {
            //已处理
            ContextCompat.getDrawable(context, R.mipmap.icon_after_sale_finished_label)
        } else {
            ContextCompat.getDrawable(context, R.mipmap.icon_after_sale_label)
        }
    }
    //是否处于售后状态
    fun inAfterSaleStatus(): Boolean {
        return ifAftersale == 1
    }
    /**
     * 是否官方发布帖子
     */
    fun isOfficial() = channel == 1
    fun requireName():String{
        return if(isOfficial()){
           "宗申骑士俱乐部官方"
        }else{
            nickName
        }
    }

    //显示颜色 官方-显示主题色 其他-显示黑色
    fun requireNameColor(context: Context): Int {
        return if (isOfficial()) {
            ContextCompat.getColor(context, R.color.color_d8000f)
        } else {
            ContextCompat.getColor(context, R.color.color_101010)
        }
    }
    //显示subName  官方-显示赛科龙官方发布 其他-显示modelName
    fun requireSubName(): String {
        return if (isOfficial()) {
            "宗申骑士俱乐部官方发布"
        } else {
            modelNickname
        }
    }
    //显示关注状态
    fun showFollowState(): Boolean {
        if (needShowFollowState == null)
            needShowFollowState = true

        // 非官方 非自己 非关注列表 才需要显示关注关系
        return channel != 1 && needShowFollowState == true && relation != UserRelationEnum.MYSELF.relation
    }

    /**
     * +关注
     */
    fun showAddIcon(): Boolean {
        return relation <= UserRelationEnum.FANS.relation
    }
    /**
     * 互关
     */
    fun showMutualIcon(): Boolean {
        return relation == UserRelationEnum.FRIENDS.relation
    }

    fun followStateName(): String {
        return when (relation) {
            UserRelationEnum.NONE.relation -> {
                "关注"
            }

            UserRelationEnum.FANS.relation -> {
                "关注"
            }

            UserRelationEnum.FOCUS.relation -> {
                "已关注"
            }

            UserRelationEnum.FRIENDS.relation -> {
                "互相关注"
            }

            else -> {
                ""
            }
        }
    }
}
/** enum */
/** 好友关系(0:无关系 1:粉丝 2:已关注 3:相互关注 4:我自己) */
enum class UserRelationEnum(val relation: Int, val typeName: String) {
    NONE(0, "无关系"),
    FANS(1, "粉丝"),
    FOCUS(2, "已关注"),
    FRIENDS(3, "互相关注"),
    MYSELF(4, "我自己"),
}

data class ImgItem(
    val url: String = "",
    val filename: String?,
    val thumbnailUrl: String?,
    val contentLength: Int?,
    val contentType: String = "",
    val imageHeight: Int = 0,
    val imageWidth: Int = 0,
    val firstImage: String = ""
)
data class TopicItem(
    val topicId: String = "",
    val topicName: String?,
)


